//
//  ESWeChatLoadingView.m
//  EasyStyle
//
//  Created by FK on 16/1/6.
//  Copyright © 2016年 FK. All rights reserved.
//

#import "ESWeChatLoadingView.h"
#import "UIImage+ESResizedImage.h"

@interface ESWeChatLoadingView()

@property(nonatomic,strong)UIImageView * loadingImageView;
@property(nonatomic,strong)UILabel     * loadingLabel;
@property(nonatomic,strong)UIButton     * loadingButton;
@property(nonatomic,strong) NSMutableArray  *arrayM;
@property (nonatomic,assign)NSInteger    tCenterY ;

@end


@implementation ESWeChatLoadingView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        _arrayM = [[NSMutableArray alloc]initWithCapacity:15];
        [self setupSubViews];
        self.backgroundColor = [UIColor colorWithHex:@"fafafa"];
    }
    return self;
}

-(void)setupSubViews
{
    UIView * view = [[UIView alloc] init];
    UIImage * image = [UIImage imageNamed:@"weChat_loding_error.png"];
    UIImageView * loadingImageView = [[UIImageView alloc] initWithImage:image];
    loadingImageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    loadingImageView.center = CGPointMake(self.width/2, loadingImageView.centerY);
    [view addSubview:loadingImageView];
    self.loadingImageView = loadingImageView;
    self.tCenterY = self.loadingImageView.centerY;
    UILabel * loadingLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(loadingImageView.frame) + 13, self.width, 17)];
    loadingLabel.text = @"加载失败了，再试试吧";
    loadingLabel.textAlignment = NSTextAlignmentCenter;
    loadingLabel.font = ESFontWithSize(16);
    loadingLabel.textColor = [UIColor colorWithHex:@"d1d1d1"];
    [view addSubview:loadingLabel];
    self.loadingLabel = loadingLabel;
    self.loadingLabel.backgroundColor = [UIColor clearColor];
    
    UIButton *loadingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loadingButton setBackgroundImage:[UIImage resizedImage:@"wuChat_loading_retry_bg01.png"] forState:UIControlStateNormal];
    [loadingButton setBackgroundImage:[UIImage resizedImage:@"wuChat_loading_retry_bg02.png"] forState:UIControlStateHighlighted];
    //    home_retry_bt02_bg02.
    [loadingButton setTitle:@"重试" forState:UIControlStateNormal];
    [loadingButton setTitleColor:[UIColor colorWithHex:@"b8b8b8"] forState:UIControlStateNormal];
    loadingButton.titleLabel.font = ESFontWithSize(16);
    loadingButton.frame = CGRectMake(0, CGRectGetMaxY(loadingLabel.frame) + 20, 130, 40);
    loadingButton.center = CGPointMake(self.width/2, loadingButton.centerY);
    [view addSubview:loadingButton];
    self.loadingButton = loadingButton;
    [loadingButton addTarget:self action:@selector(buttonTap) forControlEvents:UIControlEventTouchUpInside];
    
    view.frame = CGRectMake(0, 0, self.width, CGRectGetMaxY(loadingButton.frame));
    view.center = CGPointMake(self.width/2, self.height/2);
    [self addSubview:view];
    [self setLoadingState:1];
}

-(void)startLoading
{
    CABasicAnimation* rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    rotationAnimation.duration = 0.6;
    rotationAnimation.repeatCount = 1000;
    rotationAnimation.cumulative = NO;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [self.loadingImageView.layer addAnimation:rotationAnimation forKey:@"Rotation"];
}

-(void)stopLoading
{
    
    [self.loadingImageView.layer removeAllAnimations];
}
-(void)buttonTap
{
    if(self.buttonDidTap)
    {
        self.buttonDidTap();
    }
}
-(void)setLoadingState:(NSInteger)state
{
    self.hidden = NO;
    _state = state;
    if(state == 1)
    {
        //图有问题
        UIImage * image = [UIImage imageNamed:@"change"];
        CGPoint point = self.loadingImageView.center;
        self.loadingImageView.image = image;
        self.loadingImageView.width = image.size.width;
        self.loadingImageView.height = image.size.height;
        self.loadingImageView.center = point;
        self.loadingImageView.centerY = self.tCenterY + 30;
        self.loadingButton.hidden = YES;
        self.loadingLabel.text = @"努力加载中，请稍后";
        [self.loadingButton setBackgroundImage:[UIImage resizedImage:@"wuChat_loading_retry_bg01.png"] forState:UIControlStateNormal];
        [self.loadingButton setBackgroundImage:[UIImage resizedImage:@"wuChat_loading_retry_bg02.png"] forState:UIControlStateHighlighted];
        [self startLoading];
    }
    
}

@end
